<template>
  <div class="my-tag">
    <!-- <input
        class="input"
        type="text"
        placeholder="输入标签"
      /> -->
    <input
      type="text"
      placeholder="请输入标签"
      v-if="isEdit"
      class="input"
      v-focus
      @blur="close"
      v-model.trim="tag"
      @keyup.enter="close"
    />

    <div class="text" @dblclick="changeIsEdit" v-else>{{ value }}</div>
  </div>
</template>
  
  <script>
export default {
  props: {
    value: String,
  },
  data() {
    return {
      tag: "",
      isEdit: false,
    };
  },
  methods: {
    changeIsEdit() {
      this.isEdit = true;
      this.tag = this.value;
      //弹出输入框的时候让tag（输入框里面自己输入的值）的值等于value
    },
    close() {
      this.isEdit = false;
      this.$emit("input", this.tag);
      if (!this.tag) {
        this.tag = "暂无";
      }
      //拿到输入的tag值通过v-model传回给父亲
    },
  },
  directives: {
    focus: {
      inserted(el) {
        el.focus();
      },
    },
  },
};
</script>
  
  <style lang="less">
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>